package TestPaperSystem.service.impl;

import TestPaperSystem.Dto.RepoVo;
import TestPaperSystem.entity.Repo;
import TestPaperSystem.mapper.RepoMapper;
import TestPaperSystem.service.RepoService;
import TestPaperSystem.utils.DateGetter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements RepoService {

    @Resource
    RepoMapper repoMapper;

    @Override
    public Page<Repo> repoByUserId(RepoVo page) {

        QueryWrapper<Repo> queryWrapper = new QueryWrapper<>();

        if(page.getRepoId() == null){
            queryWrapper.lambda().eq(Repo::getUserId,page.getUserId())
                    .or()
                    .eq(Repo::getOpenType,false);
        }else {
            queryWrapper.lambda().eq(Repo::getId,page.getRepoId());
        }
        long counts = repoMapper.selectCount(queryWrapper);
        Page page1 = repoMapper.selectPage(new Page(page.getCurrentPage(), page.getSize()), queryWrapper);
        page1.setTotal(counts);
        return page1;
    }

    @Override
    public Boolean saveRepo(Repo repo) {
        UpdateWrapper<Repo> updateWrapper = new UpdateWrapper<>();
        if(repo.getId() == null){
            repo.setCreateTime(DateGetter.getInstance().getCurrentTime());
            repo.setUpdateTime(DateGetter.getInstance().getCurrentTime());
           return this.save(repo);
        }else{
            repo.setUpdateTime(DateGetter.getInstance().getCurrentTime());
            updateWrapper.lambda().eq(Repo::getId,repo.getId());
            return this.update(repo,updateWrapper);
        }
    }
}
